﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class MemberGroup
    {
        private readonly KeRui.DAL.MemberGroup dal = new KeRui.DAL.MemberGroup();

        public int Add(KeRui.Model.MemberGroup model)
        {
            return this.dal.Add(model);
        }

        public List<KeRui.Model.MemberGroup> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.MemberGroup> list = new List<KeRui.Model.MemberGroup>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.MemberGroup item = new KeRui.Model.MemberGroup();
                    if (dt.Rows[i]["GroupID"].ToString() != "")
                    {
                        item.GroupID = int.Parse(dt.Rows[i]["GroupID"].ToString());
                    }
                    item.GroupName = dt.Rows[i]["GroupName"].ToString();
                    if (dt.Rows[i]["GroupLevel"].ToString() != "")
                    {
                        item.GroupLevel = int.Parse(dt.Rows[i]["GroupLevel"].ToString());
                    }
                    item.GroupColor = dt.Rows[i]["GroupColor"].ToString();
                    if (dt.Rows[i]["EPStart"].ToString() != "")
                    {
                        item.EPStart = int.Parse(dt.Rows[i]["EPStart"].ToString());
                    }
                    if (dt.Rows[i]["EPEnd"].ToString() != "")
                    {
                        item.EPEnd = int.Parse(dt.Rows[i]["EPEnd"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    if (dt.Rows[i]["UpdateTime"].ToString() != "")
                    {
                        item.UpdateTime = DateTime.Parse(dt.Rows[i]["UpdateTime"].ToString());
                    }
                    if (dt.Rows[i]["PublishNum"].ToString() != "")
                    {
                        item.PublishNum = int.Parse(dt.Rows[i]["PublishNum"].ToString());
                    }
                    if (dt.Rows[i]["EffectiveDate"].ToString() != "")
                    {
                        item.EffectiveDate = int.Parse(dt.Rows[i]["EffectiveDate"].ToString());
                    }
                    list.Add(item);
                }
            }
            return list;
        }

        public int Delete(int GroupID)
        {
            return this.dal.Delete(GroupID);
        }

        public List<KeRui.Model.MemberGroup> GetList()
        {
            string strWhere = "";
            string strOrder = "EPStart asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("MemberGroup", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.MemberGroup> GetListByCache()
        {
            string cacheKey = "MemberGroupList";
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetList();
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (List<KeRui.Model.MemberGroup>) cache;
        }

        public KeRui.Model.MemberGroup GetModel(int GroupID)
        {
            return this.dal.GetModel(GroupID);
        }

        public KeRui.Model.MemberGroup GetModelByCache(int GroupID)
        {
            string cacheKey = "MemberGroupModel-" + GroupID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.GetModel(GroupID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("DataCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.MemberGroup) cache;
        }

        public int Update(KeRui.Model.MemberGroup model)
        {
            return this.dal.Update(model);
        }
    }
}

